package com.woniuxy.microstockserver.mapper;

import com.woniuxy.microstockserver.bean.PageRequestBean;
import com.woniuxy.microstockserver.bean.StockBean;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * @author 赵雄伟
 * 库存mapper
 */
@Mapper
public interface StockMapper {
    /**
     *分页、模糊、分类查询所有库存
     * @param pageRequestBean
     * @param
     * @return
     */
    List<StockBean> findAllStockBean(@Param("p") PageRequestBean pageRequestBean, @Param("s") StockBean stockBean);

    /**
     * 检查入库商品是否存在
     */
    @Select("select * from t_stock_info where fk_goods_id = #{goodsId}")
    StockBean findByGoodsId(@Param("goodsId") Long id);

    /**
     * 新入库：插入
     */
    @Insert("insert into t_stock_info (fk_goods_id,fk_store_id,stock_upper,stock_lower,stock_presale_amount,stock_ontheway_amount,stock_amount,stock_available,stock_remarks) values(#{s.goodsId},#{s.storeId},#{s.stockUpper},#{s.stockLower},#{s.stockPreSaleAmount},#{s.stockOnTheWayAmount},#{s.stockAmount},#{s.stockAvailable},#{s.stockRemarks})")
    @Options(useGeneratedKeys = true, keyProperty = "s.id" ,keyColumn="id")
    int saveStockBean(@Param("s") StockBean stockBean);

    /**
     * 入库：修改
     */
    @Update("<script>" +
            "update t_stock_info" +
            "<set>" +
            "<if test='s.stockAmount != null'>" +
            " stock_amount = stock_amount+#{s.stockAmount}," +
            "</if>" +
            "<if test='s.stockUpper != null'>" +
            " stock_upper = #{s.stockUpper}," +
            "</if>" +
            "<if test='s.stockLower != null'>" +
            " stock_lower = #{s.stockLower}," +
            "</if>" +
            "</set>" +
            "where fk_goods_id = #{s.goodsId}" +
            "</script>")
    int addStockBean(@Param("s") StockBean stockBean);

    /**
     * 出库：修改
     */
    @Update("<script>" +
            "update t_stock_info" +
            "<set>" +
            "<if test='s.stockAmount != null'>" +
            " stock_amount = stock_amount-#{s.stockAmount}," +
            "</if>" +
            "<if test='s.stockUpper != null'>" +
            " stock_upper = #{s.stockUpper}," +
            "</if>" +
            "<if test='s.stockLower != null'>" +
            " stock_lower = #{s.stockLower}," +
            "</if>" +
            "</set>" +
            "where fk_goods_id = #{s.goodsId}" +
            "</script>")
    int subStockBean(@Param("s") StockBean stockBean);

}
